package com.antd.admin.server.controller;

import cn.hutool.core.util.RandomUtil;
import com.antd.common.core.constant.AntdConstant;
import com.antd.common.core.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2020/12/5 15:07
 */
@RestController
@RequestMapping("/email")
public class SysEmailController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private JavaMailSender javaMailSender;
    @GetMapping("/sendEmailCode")
    public R<Object> sendEmailCode(String email){
        String code = RandomUtil.randomNumbers(6);
        SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
        simpleMailMessage.setSubject("Antd 重置密码");
        simpleMailMessage.setFrom("hugd.jelly@qq.com");
        simpleMailMessage.setTo(email);
        simpleMailMessage.setText("你好，本次重置密码的验证码为"+code+"请勿泄露");
        javaMailSender.send(simpleMailMessage);
        redisTemplate.opsForValue().set(AntdConstant.ANTD_EMAIL_CODE_KEY+email,code, 2L, TimeUnit.MINUTES);
        return R.ok("发送成功");
    }
}
